home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / imap-3.0 / non-ANSI / c-client / mbox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-14  |  7.2 KB  |  237 lines

  1. /*
  2.  * Program:    MBOX mail routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    10 March 1992
  13.  * Last Edited:    15 July 1993
  14.  *
  15.  * Copyright 1993 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36. #include <stdio.h>
  37. #include <ctype.h>
  38. #include <netdb.h>
  39. #include <errno.h>
  40. extern int errno;        /* just in case */
  41. #include "mail.h"
  42. #include "osdep.h"
  43. #include <sys/file.h>
  44. #include <sys/stat.h>
  45. #include <sys/time.h>
  46. #include "mbox.h"
  47. #include "bezerk.h"
  48. #include "misc.h"
  49.  
  50. /* MBOX mail routines */
  51.  
  52.  
  53. /* Driver dispatch used by MAIL */
  54.  
  55. DRIVER mboxdriver = {
  56.   "mbox",            /* driver name */
  57.   (DRIVER *) NIL,        /* next driver */
  58.   mbox_valid,            /* mailbox is valid for us */
  59.   bezerk_parameters,        /* manipulate parameters */
  60.   mbox_find,            /* find mailboxes */
  61.   mbox_find,            /* find bboards */
  62.   mbox_find,            /* find all mailboxes */
  63.   mbox_find,            /* find all bboards */
  64.   bezerk_subscribe,        /* subscribe to mailbox */
  65.   bezerk_unsubscribe,        /* unsubscribe from mailbox */
  66.   bezerk_subscribe_bboard,    /* subscribe to bboard */
  67.   bezerk_unsubscribe_bboard,    /* unsubscribe from bboard */
  68.   bezerk_create,        /* create mailbox */
  69.   bezerk_delete,        /* delete mailbox */
  70.   bezerk_rename,        /* rename mailbox */
  71.   mbox_open,            /* open mailbox */
  72.   bezerk_close,            /* close mailbox */
  73.   bezerk_fetchfast,        /* fetch message "fast" attributes */
  74.   bezerk_fetchflags,        /* fetch message flags */
  75.   bezerk_fetchstructure,    /* fetch message structure */
  76.   bezerk_fetchheader,        /* fetch message header only */
  77.   bezerk_fetchtext,        /* fetch message body only */
  78.   bezerk_fetchbody,        /* fetch message body section */
  79.   bezerk_setflag,        /* set message flag */
  80.   bezerk_clearflag,        /* clear message flag */
  81.   bezerk_search,        /* search for message based on criteria */
  82.   mbox_ping,            /* ping mailbox to see if still alive */
  83.   mbox_check,            /* check for new messages */
  84.   mbox_expunge,            /* expunge deleted messages */
  85.   bezerk_copy,            /* copy messages to another mailbox */
  86.   bezerk_move,            /* move messages to another mailbox */
  87.   bezerk_append,        /* append string message to mailbox */
  88.   bezerk_gc            /* garbage collect stream */
  89. };
  90.  
  91.                 /* prototype stream */
  92. MAILSTREAM mboxproto = {&mboxdriver};
  93.  
  94. /* MBOX mail validate mailbox
  95.  * Accepts: mailbox name
  96.  * Returns: our driver if name is valid, NIL otherwise
  97.  */
  98.  
  99. DRIVER *mbox_valid (name)
  100.     char *name;
  101. {
  102.   int fd;
  103.   char s[MAILTMPLEN];
  104.   char *t;
  105.   int ti,zn;
  106.   int ret = NIL;
  107.   struct stat sbuf;
  108.                 /* only consider INBOX */
  109.   if (!strcmp (ucase (strcpy (s,name)),"INBOX")) {
  110.                 /* make what the file name would be */
  111.     sprintf (s,"%s/mbox",myhomedir ());
  112.                 /* file exist? */
  113.     if ((stat(s,&sbuf) == 0) && (fd = open (s,O_RDONLY,NIL)) >= 0) {
  114.                 /* allow empty or valid file */
  115.       if ((sbuf.st_size == 0) || ((read (fd,s,MAILTMPLEN-1) >= 0) &&
  116.                   (*s == 'F') && VALID (s,t,ti,zn))) ret = T;
  117.       close (fd);        /* close the file */
  118.  
  119.     }
  120.   }
  121.   return ret ? &mboxdriver : NIL;
  122. }
  123.  
  124.  
  125. /* MBOX mail find list of mailboxes
  126.  * Accepts: mail stream
  127.  *        pattern to search
  128.  */
  129.  
  130. void mbox_find (stream,pat)
  131.     MAILSTREAM *stream;
  132.     char *pat;
  133. {
  134.   /* Always a no-op since bezerk will do this */
  135. }
  136.  
  137. /* MBOX mail open
  138.  * Accepts: stream to open
  139.  * Returns: stream on success, NIL on failure
  140.  */
  141.  
  142. MAILSTREAM *mbox_open (stream)
  143.     MAILSTREAM *stream;
  144. {
  145.   unsigned long i = 1;
  146.   unsigned long recent = 0;
  147.   char tmp[MAILTMPLEN];
  148.                 /* return prototype for OP_PROTOTYPE call */
  149.   if (!stream) return &mboxproto;
  150.                 /* change mailbox file name */
  151.   sprintf (tmp,"%s/mbox",myhomedir ());
  152.   fs_give ((void **) &stream->mailbox);
  153.   stream->mailbox = cpystr (tmp);
  154.   stream->silent = T;        /* don't babble on this stream */
  155.                 /* open mailbox, snarf new mail */
  156.   if (!(bezerk_open (stream) && mbox_ping (stream))) return NIL;
  157.   stream->silent = NIL;        /* allow external events again */
  158.                 /* notify upper level of mailbox sizes */
  159.   mail_exists (stream,stream->nmsgs);
  160.   while (i <= stream->nmsgs) if (mail_elt (stream,i++)->recent) ++recent;
  161.   mail_recent (stream,recent);    /* including recent messages */
  162.   return stream;
  163. }
  164.  
  165. /* MBOX mail ping mailbox
  166.  * Accepts: MAIL stream
  167.  * Returns: T if stream alive, else NIL
  168.  * No-op for readonly files, since read/writer can expunge it from under us!
  169.  */
  170.  
  171. long mbox_ping (stream)
  172.     MAILSTREAM *stream;
  173. {
  174.   int fd,sfd;
  175.   int ti,zn;
  176.   char *s,*t;
  177.   long size;
  178.   struct stat sbuf;
  179.   char lock[MAILTMPLEN],slock[MAILTMPLEN];
  180.   if (LOCAL && !stream->readonly && !stream->lock) {
  181.     mm_critical (stream);    /* go critical */
  182.                 /* calculate name of bezerk file */
  183.     sprintf (LOCAL->buf,MAILFILE,myusername ());
  184.     if ((sfd = bezerk_lock (LOCAL->buf,O_RDWR,NIL,slock,LOCK_EX)) >= 0) {
  185.       fstat (sfd,&sbuf);    /* get size of the poop */
  186.       if (size = sbuf.st_size){ /* non-empty? */
  187.                 /* yes, read it */
  188.     read (sfd,s = (char *) fs_get (sbuf.st_size + 1),size);
  189.     s[sbuf.st_size] = '\0';    /* tie it off */
  190.     if ((*s == 'F') && VALID (s,t,ti,zn)) {
  191.       if ((fd = bezerk_lock (stream->mailbox,O_WRONLY|O_APPEND,NIL,lock,
  192.                  LOCK_EX)) >= 0) {
  193.         fstat (fd,&sbuf);    /* get current file size before write*/
  194.         if (write (fd,s,size) >= 0) ftruncate (sfd,0);
  195.         else {        /* failed */
  196.           sprintf (LOCAL->buf,"New mail copy failed: %s",strerror (errno));
  197.           mm_log (LOCAL->buf,ERROR);
  198.           ftruncate (fd,sbuf.st_size);
  199.         }
  200.         fsync (fd);        /* force out the update */
  201.         bezerk_unlock (fd,NIL,lock);
  202.       }
  203.     }
  204.     else mm_log ("Invalid data in /usr/spool/mail file",ERROR);
  205.     fs_give ((void **) &s);    /* flush the poop now */
  206.       }
  207.                 /* all done with update */
  208.       bezerk_unlock (sfd,NIL,slock);
  209.     }
  210.     mm_nocritical (stream);    /* release critical */
  211.   }
  212.   return bezerk_ping (stream);    /* do the bezerk routine now */
  213. }
  214.  
  215. /* MBOX mail check mailbox
  216.  * Accepts: MAIL stream
  217.  */
  218.  
  219. void mbox_check (stream)
  220.     MAILSTREAM *stream;
  221. {
  222.                 /* do local ping, then do bezerk routine */
  223.   if (mbox_ping (stream)) bezerk_check (stream);
  224. }
  225.  
  226.  
  227. /* MBOX mail expunge mailbox
  228.  * Accepts: MAIL stream
  229.  */
  230.  
  231. void mbox_expunge (stream)
  232.     MAILSTREAM *stream;
  233. {
  234.   bezerk_expunge (stream);    /* do expunge */
  235.   mbox_ping (stream);        /* do local ping */
  236. }
  237.